package com.ruoyi.project.socket.packet;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import lombok.extern.slf4j.Slf4j;

/**
 * 2字节
 */
@Slf4j
public class L2Header {

    //一字节命令ID
    private byte commandId;

    //4bit (0~15)
    private byte version;

    //4bit (0~15)
    private byte reserve;

    public int getLen(){
        return 2;
    }

    public byte[] l2Header2Bytes(){
        ByteBuf buf = Unpooled.buffer(2);
        buf.writeByte(commandId);
        buf.writeByte(version*2);
        return buf.array();
    }


    public L2Header(){}

    public L2Header(byte commandId) {
        this.commandId = commandId;
    }

    private boolean checkVersion(byte version){
        if(version<0||version>15){
            log.info("l2 version只可以取0~15");
            return true;
        }
        return false;
    }
    public L2Header(byte commandId, byte version) {
        if(checkVersion(version)){
            return;
        }
        this.commandId = commandId;
        this.version = version;
    }

    public byte getCommandId() {
        return commandId;
    }

    public void setCommandId(byte commandId) {
        this.commandId = commandId;
    }

    public byte getVersion() {
        return version;
    }

    public void setVersion(byte version) {
        if(checkVersion(version)){
            return;
        }
        this.version = version;
    }

    @Override
    public String toString() {
        return "L2Header{" +
                "commandId=" + commandId +
                ", version=" + version +
                ", reserve=" + reserve +
                '}';
    }
}
